package com.gotokeep.keep.composition.source;

import android.graphics.SurfaceTexture;
import android.media.MediaCodec;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

import com.gotokeep.keep.composition.RenderSource;

import java.nio.ByteBuffer;

/**
 * @author xana/cuixianming
 * @version 1.0
 * @since 2018-04-26 10:42
 */
public abstract class MediaSource implements RenderSource {
    public static final int TYPE_VIDEO = 1;
    public static final int TYPE_AUDIO = 2;
    public static final int TYPE_IMAGE = 3;
    public static final int TYPE_GENERATOR = 4;

    // internal messages
    static final int MSG_ERROR = 0;
    static final int MSG_FRAME_AVALIABLE = 1;

    static final long TIMEOUT_US = 1000;

    public static final long DURATION_INFINATE = -1L;
    private int type;

    long durationUs = DURATION_INFINATE;
    protected int width;
    protected int height;
    private String name;
    Handler eventHandler;

    private boolean readyToRender = false;
    private boolean prepared = false;

    static ByteBuffer getInputBuffer(MediaCodec decoder, int inputIndex) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return decoder.getInputBuffer(inputIndex);
        } else {
            return decoder.getInputBuffers()[inputIndex];
        }
    }

    protected static ByteBuffer getOutputBuffer(MediaCodec decoder, int outputIndex) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return decoder.getOutputBuffer(outputIndex);
        } else {
            return decoder.getOutputBuffers()[outputIndex];
        }
    }

    public MediaSource(int type, String name, Handler eventHandler) {
        this.type = type;
        this.name = name;
        this.eventHandler = eventHandler;
    }

    public int getType() {
        return type;
    }

    public long getDurationUs() {
        return durationUs;
    }

    public void setDurationUs(long durationUs) {
        this.durationUs = durationUs;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public boolean isPrepared() {
        return prepared;
    }

    public boolean isReadyToRender() {
        return readyToRender;
    }

    public String getName() {
        return name;
    }
}
